<template>
  <div>
    <el-form ref="form" :model="insertProduct" label-width="80px" class="edit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品名称 :">
            {{insertProduct.name}}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品编码 :">
            {{insertProduct.code}}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品线 :">
            {{insertProduct.productLine.name}}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品类 :">
            {{insertProduct.category.name}}
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="详情图片 :">
        <el-upload
          list-type="picture-card"
          ref="upload"
          :action= action
          :headers="myHeaders"
          :file-list="fileList"
          :show-file-list="true"
          :auto-upload="false"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
    </el-form>
<!--    <div>
      <input type="file" id="file"></input>
      <button @click="upload()">点击上传</button>
    </div>-->
    <el-button type="success" @click="submitUpload" class="btnConserveAdd">上传</el-button>
    <el-button type="primary" @click="closePicture()" class="btnCloseAdd">退出</el-button>
  </div>
</template>

<script>
export default {
  name: "uploadPicture",
  props:{
    insertProduct: Object
  },
  data(){
    return {
      //图片
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      myHeaders: {authorization: sessionStorage.getItem("token")},
      action: this.baseUrl+'file/upload',
      fileList:[],
      productImage:{}
    }
  },
  methods:{

    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
      console.log(this.dialogImageUrl)
    },

    handleAvatarSuccess(res, file) {
      console.log(file)
      console.log(res.data)
      this.productImage = res.data;
      //将产品id存储到productImage对象中
      this.$set(this.productImage, "productId", this.insertProduct.id);
      console.log(this.productImage);
      //发送请求存储到数据库
      this.axios.post(this.baseUrl+"product/insertImage",this.productImage)
        .then(res=>{
          console.log(res.data);
          if("success"===res.data.message){
            this.$message({
              type: 'success',
              message: res.data.data
            });
          }else {
            this.$alert(res.data.data, '警告', {
              confirmButtonText: '确定',
              callback: action => {
                this.$message({
                  type: 'warning',
                  message: res.data.data
                });
              }
            });
          }
        });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/png" || "image/jpg" || "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG/JPEG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    closePicture(){
      this.$emit('closePicture');
    },
    /*upload(){
      let file = document.getElementById("file").files[0];
      console.log(file);
      let formdata = new FormData();
      formdata.append("file",file);

      this.axios.post(this.baseUrl+"file/upload/",formdata,{
        headers:{
          'Content-Type':'multipart/form-data'
        }
      }).then(res=>{
        console.log(res.data)
      })
    }*/
  }
}
</script>

<style scoped>
@import "../../../assets/css/basicdata.css";
</style>
